package com.zksk.portal.service.invoice.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zksk.common.core.utils.StringUtils;
import com.zksk.mbg.admin.domain.MemberInvoiceRecord;
import com.zksk.mbg.admin.domain.Orders;
import com.zksk.mbg.admin.mapper.OrdersMapper;
import com.zksk.portal.domain.invoice.dto.InvoiceApplyDto;
import com.zksk.portal.domain.invoice.vo.InvoiceApplyVo;
import com.zksk.portal.mapper.invoice.InvoiceApplyDao;
import com.zksk.portal.service.invoice.InvoiceApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class InvoiceApplyServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements InvoiceApplyService {

    @Autowired
    private InvoiceApplyDao invoiceApplyDao;

    @Override
    public IPage<InvoiceApplyVo> invoiceList(InvoiceApplyDto invoiceApplyDto) {
        IPage<InvoiceApplyVo> pageConf = new Page<>(invoiceApplyDto.getPageNum(), invoiceApplyDto.getPageSize());

        QueryWrapper<Orders> queryWrapper = new QueryWrapper<Orders>()
                .eq(invoiceApplyDto.getStatus() !=null && invoiceApplyDto.getStatus() !=3, "mir.status", invoiceApplyDto.getStatus()) // 开票状态
                .notExists(invoiceApplyDto.getStatus() !=null && invoiceApplyDto.getStatus() ==3,
                        "select io.orders_id from platform_fact_orders io where io.orders_id = mir.orders_id")
                .eq(invoiceApplyDto.getMemberId() != null, "o.member_id", invoiceApplyDto.getMemberId()) // 企业id
                .eq(invoiceApplyDto.getGuaranteeType() != null, "og.guarantee_type", invoiceApplyDto.getGuaranteeType()) //保函类型
                .like(StringUtils.isNotEmpty(invoiceApplyDto.getSectionName()), "o.section_name", invoiceApplyDto.getSectionName()) // 标段名称
                .between(invoiceApplyDto.getOrdersTimeL() != null && invoiceApplyDto.getOrdersTimeR() != null, "o.create_time",
                        invoiceApplyDto.getOrdersTimeL(), invoiceApplyDto.getOrdersTimeR()) // 订单时间
                .last("and o.del_flag='0' and o.orders_status='0' order by o.create_time desc");

        return invoiceApplyDao.invoiceList(pageConf, queryWrapper);
    }
}
